package com.lancoo.answer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lancoo.answer.R;

import java.text.SimpleDateFormat;


public class VideoPlayView extends RelativeLayout implements View.OnClickListener,View.OnLongClickListener {
    private ImageView iv_music;
    private boolean isPlaying = false;
    private String videoUrl;
    private IVideoClick iVideoClick;
    private RelativeLayout rl_main;
    private long time ;
    private TextView tv_time;
    private ImageView iv_play;
    private String thumb;


    public void setiVideoClick(IVideoClick iVideoClick) {
        this.iVideoClick = iVideoClick;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public VideoPlayView(Context context) {
        super(context);
        init(context);
    }

    public VideoPlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VideoPlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        LayoutInflater.from(context).inflate(R.layout.ev_video_play_layout,this);
        iv_music = findViewById(R.id.iv_display);
        tv_time = findViewById(R.id.tv_time);
        iv_play = findViewById(R.id.iv_play);
        iv_play.setOnClickListener(this);
        iv_play.setOnLongClickListener(this);
        findViewById(R.id.fl_Wave).bringToFront();
        findViewById(R.id.fl_Wave).setOnClickListener(this);
        findViewById(R.id.fl_Wave).setOnLongClickListener(this);
    }


    public void reFresh(String url,boolean isPlaying,long time,String thumb){

        this.videoUrl = url;
        this.isPlaying = isPlaying;
        this.time = time;
        this.thumb = thumb;
        Log.e("aaaa","传进来视频时长："+time);
        tv_time.setText(getFormatMMSS(time));  //参数要求时毫秒级别
//        iv_music.setImageResource(com.lancoo.klgijklib.R.mipmap.ijk_video_not_net);
//        Glide.with(getContext()).load(thumb).apply(RequestOptions.placeholderOf(com.lancoo.klgijklib.R.mipmap.ijk_video_not_net)).into(new SimpleTarget<Drawable>() {
//            @Override
//            public void onResourceReady(@NonNull @NotNull Drawable resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
//                BitmapDrawable drawable = (BitmapDrawable) resource;
//
//                Log.e("aaa","宽"+ drawable.getBitmap().getWidth());
//                Log.e("aaa","高"+ drawable.getBitmap().getHeight());
//                iv_music.setImageDrawable(drawable);
//            }
//        });
        Glide.with(getContext()).load(thumb).apply(RequestOptions.placeholderOf(R.mipmap.ev_video_not_net)).into(iv_music);
    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.fl_Wave || id == R.id.iv_play){
            if (iVideoClick!=null){
                iVideoClick.onVideoClick(videoUrl,!isPlaying);
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (iVideoClick!=null){
            iVideoClick.onVideoLongClick(videoUrl);
        }
        return false;
    }


    public interface IVideoClick{
        void onVideoClick(String url,boolean isPlaying);

        void onVideoLongClick(String url);
    }

    public  String getFormatMMSS(long needFromatTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        String formatTime = formatter.format(needFromatTime);
        return formatTime;
    }

}
